package com.example.myapplication.hospital;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClassificationofdoctorsBean;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ConsultationpageActivity extends AppCompatActivity {

    private String CONSULTATIONPAGE_ID;
    private ImageView bloak;
    private TextView name;
    private TextView jobName;
    private TextView practiceNo;
    private TextView problem;
    private TextView imgsubmit;
    private LinearLayout li_submit;
    private ClassificationofdoctorsBean.RowsBean rowsBean;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consultationpage);
        initview();
        //返回
        but_bloak();
        //填充医生信息
        doctorinformation();
        //提交信息
        submit();
    }

    private void submit() {
        li_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String problemDate = problem.getText().toString();
                JSONObject jsonObject = new JSONObject();
                Integer id = 0;
                if (rowsBean != null) {
                    if (!TextUtils.isEmpty(String.valueOf(rowsBean.getId()))) {
                        id = rowsBean.getId();
                    }
                }
                if (problemDate == null ||  "".equals(problemDate)) {
                    Toast.makeText(ConsultationpageActivity.this, "提问的问题不能为空", Toast.LENGTH_SHORT).show();
                }


                if (id == 0) {
                    Toast.makeText(ConsultationpageActivity.this, "id不能为空", Toast.LENGTH_SHORT).show();
                }

                if(problemDate != null && !"".equals(problemDate) && id != 0){
                    try {
                        jsonObject.put("doctorId", id);
                        jsonObject.put("question", problemDate);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String JSON = jsonObject.toString();
                    submitBean(JSON);
                }
            }
        });
    }

    private void submitBean(String JSON) {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean) obj;
                                if (leavecommentBean.getCode() == 200) {
                                    Toast.makeText(ConsultationpageActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }, JSON);
    }

    private void doctorinformation() {


        if (rowsBean != null) {
            if (!TextUtils.isEmpty(rowsBean.getAvatar())) {
                Glide.with(ConsultationpageActivity.this).load(TokenBean.IP + rowsBean.getAvatar()).into(img);
            }

            if (!TextUtils.isEmpty(rowsBean.getName())) {
                name.setText(rowsBean.getName());
            }

            if (!TextUtils.isEmpty(rowsBean.getJobName())) {
                jobName.setText(rowsBean.getJobName());
            }

            if (!TextUtils.isEmpty(rowsBean.getPracticeNo())) {
                practiceNo.setText(rowsBean.getPracticeNo());
            }
        }
    }

    private void but_bloak() {
        bloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConsultationpageActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        CONSULTATIONPAGE_ID = intent.getStringExtra(Commom.CONSULTATIONPAGE_ID);
        Gson gson = new Gson();
        rowsBean = gson.fromJson(CONSULTATIONPAGE_ID, ClassificationofdoctorsBean.RowsBean.class);
        bloak = findViewById(R.id.bloak);
        name = findViewById(R.id.name);
        jobName = findViewById(R.id.jobName);
        practiceNo = findViewById(R.id.practiceNo);
        problem = findViewById(R.id.problem);
        imgsubmit = findViewById(R.id.imgsubmit);
        li_submit = findViewById(R.id.li_submit);
        img = findViewById(R.id.img);
    }
}